﻿using Dapper;
using HR.IServices.PengIService;
using HR.Models.PengModels;
using System.Data.SqlClient;

namespace HR.Services.PengService
{
    public class ZhiWeiServices : IZhiWeiServices
    {
        private readonly IConfiguration conf;
        private readonly string? conStr;

        public ZhiWeiServices(IConfiguration conf)
        {
            this.conf = conf;
            conStr = this.conf["ConnectionStrings"];
        }
        public Task<int> Add(ZhiWei zhiWei)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr)) 
                {
                    return con.Execute($"insert into config_major(major_kind_id, major_kind_name, major_id, major_name, test_amount) values('{zhiWei.Major_kind_id}','{zhiWei.Major_kind_name}','{zhiWei.Major_id}','{zhiWei.Major_name}','{zhiWei.Test_amount}')");
                }
            });
        }

        public Task<int> Delete(int mak_id)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Execute($"delete from config_major where mak_id='{mak_id}'");
                }
            });
        }

        public Task<List<ZhiWei>> FindAll()
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Query<ZhiWei>($"select * from config_major").ToList();
                }
            });
        }

        public Task<int> Update(ZhiWei zhiWei)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Execute($"update config_major set major_kind_id='{zhiWei.Major_kind_id}',major_kind_name='{zhiWei.Major_kind_name}',major_id='{zhiWei.Major_id}',major_name='{zhiWei.Major_name}',test_amount='{zhiWei.Test_amount}' where mak_id='{zhiWei.Mak_id}'");
                }
            });
        }
    }
}
